package com.example.communitypro.service.impl;

import com.example.communitypro.mapper.UserDetailsMapper;
import com.example.communitypro.pojo.UserDetails;
import com.example.communitypro.service.UserDetailsService;
import com.example.communitypro.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired(required=false)
    private UserDetailsMapper userDetailsMapper;


    @Override
    public Result addUserDetails(UserDetails record){
        try {
            if(userDetailsMapper.insertSelective(record)>0)
            {
                return Result.ok().message("新增用户详细信息成功");
            }
            else{
                return Result.error().message("新增用户详细信息失败");
            }
        }catch (Exception e){
            return Result.error().message("新增用户详细信息失败");
        }
    }


    @Override
    public Result dropUserDetails(int userInformationNo){
        try {
            if(userDetailsMapper.deleteByPrimaryKey(userInformationNo)>0)
            {
                return Result.ok().message("删除用户详细信息成功");
            }
            else{
                return Result.error().message("删除用户详细信息失败");
            }
        }catch (Exception e){
            return Result.error().message("删除用户详细信息失败");
        }
    }

    @Override
    public Result modifyUserDetails(UserDetails record){
        try {
            if(userDetailsMapper.updateByPrimaryKeySelective(record)>0)
            {
                return Result.ok().message("修改成功");
            }
            else{
                return Result.error().message("修改失败：未找到对应记录");
            }
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryUserDetails(int userInformationNo){
        try {
            UserDetails userDetails=userDetailsMapper.selectByPrimaryKey(userInformationNo);
            if(userDetails!=null)
            {
                return Result.ok().message("查找用户详细信息成功").data("userDetails",userDetails);
            }
            else{
                return Result.error().message("查找用户详细信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户详细信息失败");
        }
    }

    @Override
    public Result queryByUser(int user_no) {
        try {
            UserDetails userDetails=userDetailsMapper.selectByUser(user_no);
            if(userDetails!=null)
            {
                return Result.ok().message("查找用户详细信息成功").data("userDetails",userDetails);
            }
            else{
                return Result.error().message("查找用户详细信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户详细信息失败");
        }
    }

    @Override
    public Result selectAll() {
        try {
            List<UserDetails> userDetails=userDetailsMapper.selectAll();
            if(userDetails!=null)
            {
                return Result.ok().message("查找用户详细信息成功").data("userDetails",userDetails).count(userDetails.size());
            }
            else{
                return Result.error().message("查找用户详细信息失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户详细信息失败");
        }
    }
}
